//加载页面CSS
if (_isMobile) {
	if (location.pathname.indexOf("m.") !== -1) {
		var cssUrlList = ["r_css!css/m.default.css"]
	} else if (location.pathname.indexOf("mobile.index.html") !== -1) {
		var cssUrlList = ["r_css!css/mobile.index.css"]
	} else {
		var cssUrlList = []
	}
} else {
	var cssUrlList = ["r_css!css/default-beta.css"]
}
require(cssUrlList);
define("core", ["common", "jQuery", "r_css", "jQuery.notify", "appConfig"], function(jSouper, $, cssAPI) {

	require(["coAjax", "Cookies", "href", "queryString", "jSouper_page", "title", "page_onload", "eventManager"], function(coAjax, Cookies, href, QueryString, jSouper_page, title, page_onload, eventManager) {
		//主页类型跳转
		if (_isStore) {
			var pathname = href.pathnameFix(location.pathname);
			if (pathname === "/" || pathname == "/main-beta.html") {
				try {
					var management_category = appConfig.site_info.permission.data_management_category;
				} catch (e) {
					management_category = "商店";
				}
				var management_category_pathname_map = {
					"商店": "",
					"餐饮": "/catering.main.html"
				}
				var new_pathname = management_category_pathname_map[management_category];
				new_pathname && (location.pathname = new_pathname);
			}
		}
		/*
		 * 记录推荐者、导购者、微信OPENID
		 */
		var queryString = new QueryString();
		var hashQS = new QueryString(location.hash);
		var recommender_id = queryString.get("rcid") || hashQS.get("rcid");
		var guide_id = queryString.get("gi") || hashQS.get("gi");
		var wx_openid = window.WEIXIN_OPENID = queryString.get("WEIXIN_OPENID") || hashQS.get("WEIXIN_OPENID");
		if (recommender_id) {
			Cookies.set("recommender_id_bak", Cookies.get("recommender_id"));
			Cookies.set("recommender_id", recommender_id);
			eventManager.is(App.get("loginer"), "getLoginer", function() {
				var loginer = App.get("loginer");
				if (loginer._id == recommender_id) {
					Cookies.set("recommender_id", Cookies.get("recommender_id_bak"));
				}
			});
		}
		if (guide_id) {
			Cookies.set("guide_id_bak", Cookies.get("guide_id"));
			Cookies.set("guide_id", guide_id);
			eventManager.is(App.get("loginer"), "getLoginer", function() {
				var loginer = App.get("loginer");
				if (loginer._id == guide_id) {
					Cookies.set("guide_id", Cookies.get("guide_id_bak"));
				}
			});
		}
		//微信OPENID
		if (wx_openid) {
			//TOP.html 里面的自动登录脚本运行过了，没有global.WEIXIN_OPEN，也没有lib-LS的OPENID数据，肯定是无法进行微信登录，这时候重新运行一下
			window.coAjaxLoginUser && window.coAjaxLoginUser();
			//保存到lib里面，确保跨页面使用
			globalSet("WEIXIN_OPENID:" + appConfig.bus_id, wx_openid, function() {
				alert("success", "微信用户授权成功");
				eventManager.is(App.get("loginer"), "getLoginer", function() {
					//如果已经绑定微信账号，就不管，要就去个人中心实现“重新绑定”
					if (App.get("loginer.info.weixin_unionid")) {
						return;
					}
					myConfirm("是否将当前登录账号与微信账号做绑定？", function() {
						coAjax.put(appConfig.user.bind_weixin_account_by_open_id, {
							openid: wx_openid
						}, function(result) {
							App.set("loginer", result.result);
							alert("success", "微信号绑定成功，下次无需手动登录");
						});
					}, function() {
						alert("绑定已经取消");
					});
				});
			});
		}
		/*
		 * 记录访客
		 */
		if (!href.isAdmin()) {
			coAjax.post(appConfig.recordVisitorInfo, {
				bus_id: appConfig.bus_id
			}, function(result) {
				console.log("访客信息已经记录：", result.result);
			});
		} else {
			console.log("管理员页面，访客信息不记录");
		}

		/*
		 * 初始化窗口信息
		 */
		title();
		/*
		 * 整理配置信息
		 */
		App.set("bus_info", appConfig.site_info);
		App.set("config", appConfig);
		/*
		 * 开启分页功能
		 */
		jSouper_page();
	});
	// /*
	//  * 使用History API实现无刷新跳转
	//  */
	// if (window.history && history.pushState) {
	// 	var _ajax_get_page;
	// 	var _current_page_info;
	// 	var _map_cache = {};
	// 	require(["jQuery", "href"], function($, href) {
	// 		$("body").on("click", "a", function(e) {
	// 			if (location.host == this.host) {
	// 				href.jump(this.href);
	// 				e.preventDefault();
	// 				return false;
	// 			}
	// 		});
	// 	});
	// }
	/*
	 * 重写弹出框
	 */
	require(["color"], function(color) {
		var _alert = window.native_alert = window.alert;
		window.alert = function() {
			var args = arguments;
			var result;
			if (args.length === 1) {
				var alert_str = args[0];
				if (typeof alert_str === "string") {
					result = $.Notify.show(alert_str)
				} else {
					result = $.Notify(alert_str);
				}
			} else if (args.length >= 2) {
				var type = args[0];
				var alert_str = args[1];
				var time = (args[2] === undefined) ? 3000 : args[2];
				var class_color = color[type];
				result = $.Notify({
					style: {
						background: class_color,
						color: "white"
					},
					content: alert_str,
					timeout: time
				});
			}
			return result;
		}
	});
	/*
	 * 版本管理
	 */
	require(["versionManage"], function(versionManage) {
		versionManage("o2o_version", "appConfig");
	});
	return jSouper;
});